home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / exec.arc / EF.C next >
Text File  |  1985-05-28  |  1KB  |  64 lines

  1. /*    ef.c        execute a list of commands in a file 
  2.                 demo of the fork/execf functions for CI-C86 2.0+
  3.  
  4.             usage: ef filename.ext    filename.ext should contain
  5.                                             a list of commands just like
  6.                                             a batch file
  7.  
  8.             !copyrighted by James Montebello. 
  9.                 
  10. */
  11.  
  12. #include <stdio.h>
  13.  
  14. main(ac,av)
  15. int ac;
  16. char *av[];
  17. {
  18.     char *name;
  19.     char cmdl[255];
  20.     char *sp;
  21.     FILE *fp;
  22.     int status;
  23.  
  24.     if (!(fp=fopen(av[1],"r"))) {
  25.         printf("ef: error reading file %s\n",av[1]);
  26.         exit(1);
  27.     }
  28.     sp = alloc(255);
  29.     while (getline(sp,fp)) {
  30.         if (blankline(sp)) continue;        /* line blank? skip it */
  31.         printf("\n%s\n",sp);
  32.         if (fork(sp)) {                        /* launch the command */
  33.             printf("ef: error on line %s\n",sp);    /* return non-zero, err */
  34.             exit(1);
  35.         }
  36.     }
  37. }
  38.  
  39. getline(s,fp)        /* gets a \n termed line from fp, rets NULL at EOF */
  40. char s[];
  41. FILE *fp;
  42. {
  43.     int i;
  44.     char c;
  45.  
  46.     i = 0;
  47.     while ((c=getc(fp)) != EOF) {
  48.         s[i++] = c;
  49.         if (c == '\n')    
  50.             break;
  51.     }
  52.     s[i-1] = '\0';            /* don't want the \n in the line */
  53.     return(i);
  54. }
  55.  
  56. blankline(s)        /* rets TRUE if line is all whitespace */
  57. char *s;
  58. {
  59.     while(*s) {
  60.         if (!isspace(*s++)) return 0; /* found something, blast out */
  61.     }
  62.     return -1;        /* its empty */
  63. }
  64.